import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:login/config/SystemConfig.dart';
import 'package:login/util/UserInfo.dart';
import 'dart:convert';

class NearbyPage extends StatefulWidget {
  NearbyPage({Key key}) : super(key: key);
  @override
  _NearbyPageState createState() => _NearbyPageState();
}

class _NearbyPageState extends State<NearbyPage> {
  //数据查询的列表，为我点赞最多的用户前6名
  List _nearUserImageList;

  //用户信息
  Map userInfo;
 @override
  void initState() {
    super.initState();
    this._init();
  }

  void _init() async {
    Map tmepUserInfo = await UserInfo.getUserInfo();
    setState(() {
      //
      userInfo = tmepUserInfo;
    });

    //查询数据
    _queryList();
  }

  void _queryList() async {
    //请求后台
    String api = SystemConfig.BASE_URL +
        '/comment/listMyTopCommentUser?appUserId=${userInfo['id']}'; //id从storage.dart中获取data
    print(api + "+++++====");
    //向后台发生请求
    Response response = await Dio().get(api);

    //respons包含数据状态码什么的,用json.decode
    Map map = json.decode(response.toString()); //整个键值对
    if (response.statusCode == 200 && map != null && map['success']) {
      setState(() {
        //回调函数
        this._nearUserImageList = map['data'];
      });
    }
  }

  //显示自己
  Widget _buildSelfImage(double x, double y, double size, String url) {
    //传参
    return Align(
      //剧中
      alignment: Alignment(x, y),
      child: Container(
        width: size,
        height: size,
        child: ClipOval(
          child:(userInfo !=null && this.userInfo['headImgUrl'].toString() !=null) 
             ?Image.network(
              SystemConfig.BASE_URL + '/' + userInfo['headImgUrl'],
              fit: BoxFit.cover)
              :Image.asset(url, fit: BoxFit.cover),
        ),
      ),
    );
  }

  //
  Widget _buildUsersImage(
      double x, double y, double size, String url, Map item) {
    return Align(
      //定位
      alignment: Alignment(x, y),
      //显示头像和昵称
      child: Stack(
        children: <Widget>[
          //第一层
          Container(
            width: size,
            height: size,
            child: ClipOval(
              child: (item != null && item['headImgUrl'] != null)
                  ? Image.network(SystemConfig.BASE_URL + item['headImgUrl'],
                      fit: BoxFit.cover)
                  : Image.asset(url, fit: BoxFit.cover),
            ),
          ),
          //第二层
          Container(
            width: size,
            height: size,
            alignment: Alignment.topRight,
            child: Container(
              width: 18.0,
              height: 18.0,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: Colors.red,
                border: Border.all(color: Colors.white, width: 1.0),
                borderRadius: BorderRadius.circular(9.0),
              ),
              child: Text(
                  (item != null && item['commentCnt'].toString() != null)
                      ? item['commentCnt'].toString()
                      : '',
                  style:
                      TextStyle(fontSize: 12.0, fontWeight: FontWeight.bold)),
            ),
          ),
          //第 三层
        ],
      ),
    );
  }

  //自定义背景图片组件
  Widget _buildBackGroundImage(double x, double y, double size, String url) {
    return Align(
        alignment: Alignment(x, y),
        child: Container(
          width: size,
          height: size,
          child: ClipOval(
            child: Image.asset(url, fit: BoxFit.cover),
          ),
        ));
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
              image: AssetImage('images/tt.gif'),fit: BoxFit.cover
          ),
        ),
        child: Stack(
          //叠加布局
          children: <Widget>[
            //显示自己的图像
            this._buildSelfImage(0, 0, 60, 'images/e.jpg'),

            //显示离我最近的6个用户头像
            this._buildUsersImage(
                0.25,
                0.25,
                50,
                'images/noheadimg.jpg',
                (this._nearUserImageList != null &&
                        this._nearUserImageList.length > 0)
                    ? this._nearUserImageList[0]
                    : null),
            this._buildUsersImage(
                -0.25,
                0.25,
                50,
                'images/noheadimg.jpg',
                (this._nearUserImageList != null &&
                        this._nearUserImageList.length > 1)
                    ? this._nearUserImageList[1]
                    : null),

            this._buildUsersImage(
                0.25,
                -0.25,
                50,
                'images/noheadimg.jpg',
                (this._nearUserImageList != null &&
                        this._nearUserImageList.length > 2)
                    ? this._nearUserImageList[2]
                    : null),
            this._buildUsersImage(
                -0.25,
                -0.25,
                50,
                'images/noheadimg.jpg',
                (this._nearUserImageList != null &&
                        this._nearUserImageList.length > 3)
                    ? this._nearUserImageList[3]
                    : null),
            this._buildUsersImage(
                0.5,
                0,
                50,
                'images/noheadimg.jpg',
                (this._nearUserImageList != null &&
                        this._nearUserImageList.length > 4)
                    ? this._nearUserImageList[4]
                    : null),
            this._buildUsersImage(
                -0.5,
                0,
                50,
                'images/noheadimg.jpg',
                (this._nearUserImageList != null &&
                        this._nearUserImageList.length > 5)
                    ? this._nearUserImageList[5]
                    : null),
            //显示离我第二近的4个用户
            this._buildBackGroundImage(
                0.6, 0.6, 40, 'images/star_decroate3.png'),
            this._buildBackGroundImage(
                -0.6, 0.6, 40, 'images/star_decroate3.png'),
            this._buildBackGroundImage(
                0.6, -0.6, 40, 'images/star_decroate3.png'),
            this._buildBackGroundImage(
                -0.6, -0.6, 40, 'images/star_decroate3.png'),
            //显示离我第三近的两个用户
            this._buildBackGroundImage(
                0, -0.85, 30, 'images/star_decroate.png'),
            this._buildBackGroundImage(0, 0.85, 30, 'images/star_decroate.png'),
            //第四
            this._buildBackGroundImage(
                -0.9, 0.9, 20, 'images/star_decroate.png'),
            this._buildBackGroundImage(0, 0.5, 20, 'images/star_decroate.png'),
            //显示离我最远的1个
            this._buildBackGroundImage(
                0.95, 0.95, 10, 'images/star_decroate.png'),
          ],
        ),
      ),
    );
  }
}
